"""
简化版家庭记账数据模型
"""

from tortoise import fields
from app.models.base.base import BaseModel
from enum import Enum
from app.models.family.family_system import FamilyMember,Family


class AccountType(str, Enum):
    """账单类型"""
    INCOME = "income"    # 收入
    EXPENSE = "expense"  # 支出



class AccountRecord(BaseModel):
    """账单记录模型"""
    amount = fields.DecimalField(max_digits=12, decimal_places=2, description="金额")
    type = fields.CharField(max_length=10, description="类型: income/expense")
    category = fields.CharField(max_length=50, description="分类")
    subcategory = fields.CharField(max_length=50, null=True, description="子分类")
    remark = fields.CharField(max_length=255, null=True, description="备注")
    record_date = fields.DateField(description="记录日期")
    
    # 关联关系
    user = fields.ForeignKeyField('models.User', related_name='account_records',null=True)
    family = fields.ForeignKeyField('models.Family', related_name='account_records',null=True)
    
    # 可选的详细信息
    location = fields.CharField(max_length=100, null=True, description="消费地点")
    tags = fields.JSONField(default=list, description="标签")
    
    class Meta:
        table = "account_records"
        table_description = "账单记录表"
        indexes = [("family", "record_date"), ("user", "record_date")]
        
    def __str__(self):
        return f"AccountRecord(id={self.id}, amount={self.amount}, type={self.type})"
    
    async def to_dict(self):
        return {
            "id": self.id,
            "amount": float(self.amount),
            "type": self.type,
            "category": self.category,
            "subcategory": self.subcategory,
            "remark": self.remark,
            "record_date": self.record_date.isoformat() if self.record_date else None,
            "user_id": self.user_id,
            "family_id": self.family_id,
            "location": self.location,
            "tags": self.tags,
            "created_at": self.created_at.isoformat() if self.created_at else None,
        }